/*
 * Copyright (C) 2007,2008  Jonathan Hendler
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * 
 * The authors can be contacted at: 
 * @author Jonathan Hendler [jonathan DOT hendler AT google DOT com]
 * 
 * @license GPLv2
 */

#ifndef NYMRL_STATIC_SET_
#define NYMRL_STATIC_SET_
#include <time.h>
#include <unistd.h>
#include "sqlite_helper.h"
#include "nymrl_message.h"

//TODO make configurable
#define NYMRL_ROUTER_DB "/home/www/db/nymrl.db"
#define NYMRL_ROUTER_LOGS "/home/www/db/nymrl_logs.db"

#define NYMRL_ROUTER_DB_TABLE_NODES "node"
#define NYMRL_ROUTER_DB_TABLE_NODES_X_URL "node_x_url"
#define NYMRL_ROUTER_DB_TABLE_URL "url"

#define NYMRL_LOG_DB_TABLE "logs"
#define NYMRL_ROUTER_CONFIGURATION_TABLE "config"
#define NYMRL_ADMIN_HOST_VAR "nymrl_admin_server"


sqlite3 * nymrl_get_router_db();
sqlite3 * nymrl_get_log_db();
int nymrl_sqlite_global_close();

//void nymrl_get_db();
void _nymrl_db_log(const char * type, char * message);
void nymrl_log_error(char * message);
void nymrl_log_message(char * message);

inline char * nyml_get_config(const char * key);
inline int nyml_set_config(const char * key, char * value);
inline int nyml_assert_config(const char * key, char * expected_value);

//admin
inline int nyml_set_admin_url(char * url);
inline char * nyml_get_admin_url();

inline NYMRL_URL nymrl_get_self(char * rel);
inline int nymrl_set_self(char * rel, NYMRL_URL this_url);

//nid
//helpers - DB specific
inline  int _nyml_remove_url_from_node( NYMRL_ID id,NYMRL_URL url);
inline  int _nyml_remove_node(NYMRL_ID nid);
inline  NYMRL_ID _nyml_create_node(NYMRL_ID nid, char * label);
inline  int _nyml_remove_url(NYMRL_URL url);
inline  int _nyml_add_node(NYMRL_ID id, NYMRL_URL url, char * rel);
inline  int _nyml_update_node(NYMRL_ID id, NYMRL_URL url, char * rel);
/**
 * get all the urls for a group
 * 0 for all, 1 for any, 2 for any two
 * @todo algorithm can determine "closest"
 * @todo min and max
 * */

inline NYMRL_URL_LIST _nymrl_get_urls(int url_count, NYMRL_ID nid, char * rel, int * row_count);
/**
 * api function for recieving special messages from the PHP admin.
 * returns base64 encoded string, maybe JSON
 * */
inline char * nymrl_admin_execute_base_64_query(char * encoded_sql , char * db_name);

int nymrl_sqlite_print_all_nymrl_data();

//SETUP
int nyml_setup_self(int drop_tables);



#endif